/*
 * Copyright (C) 2009 IMOBISOFT Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package imobisoft.android.hialarm.application;

import android.app.TimePickerDialog;
import android.content.Intent;
import android.database.ContentObserver;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.preference.ListPreference;
import android.preference.PreferenceActivity;
import android.widget.TimePicker;

import imobisoft.android.hialarm.R;
import imobisoft.android.hialarm.base.AlarmConst;

/**
 * 
 * Setting screen for Open/close clock
 *
 */
public class SetAlarm extends PreferenceActivity
        implements AlarmConst.AlarmSettings, TimePickerDialog.OnTimeSetListener {
	/**
	 * Store alarm ringtones;
	 */
	private Cursor mRingtoneCursor;
	
	private ContentObserver mAlarmsChangeObserver;
	private AlarmPreference  mAlarmPref;
	/**
	 * alarm id in database
	 */
	 private int mId;
	 
	 /**
	  * 
	  * Notify the client with the db change of alarm records
	  *
	  */
	 private class AlarmsChangeObserver extends ContentObserver {
		public AlarmsChangeObserver() {
			super(new Handler());
		}

		@Override
		public void onChange(boolean selfChange) {
			AlarmConst.getAlarm(getContentResolver(), SetAlarm.this, mId);
		}
	}
	
	  /**
     * Set an alarm.  Requires an Alarms.ID to be passed in as an
     * extra
     */
    @Override
    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        
        addPreferencesFromResource(R.xml.alarm_prefs);
        mAlarmPref = (AlarmPreference)findPreference("alarm");
        
        Intent i = getIntent();
        mId = i.getIntExtra(AlarmConst.ID, -1);
        
        AlarmConst.getAlarm(getContentResolver(), this, mId);
        AlarmConst.getRingtones(this, this);
        
        mAlarmsChangeObserver = new AlarmsChangeObserver();
        getContentResolver().registerContentObserver(
                AlarmConst.AlarmColumns.CONTENT_URI, true, mAlarmsChangeObserver);
    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
        getContentResolver().unregisterContentObserver(mAlarmsChangeObserver);
    }

	@Override
	public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
		
	}
	
	/**
     * Alarms.AlarmSettings implementation.  Database feeds current
     * settings in through this call
     */
    public void reportAlarm(
            int idx, boolean enabled, int hour, int minutes,
            AlarmConst.DaysOfWeek daysOfWeek, boolean vibrate,String message,
            String alert) {
    }
    
    /**
     * Alarms.AlarmSettings implementation.  Database feeds current
     * ringtones in through this call
     */
    public void reportRingtones(Cursor cursor){
    	mRingtoneCursor = cursor;
    	startManagingCursor(mRingtoneCursor);
    	mAlarmPref.setCursor(cursor);
    	
    }
}